﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracy.BasicServices;
using Tracy.DataModel;
using Tracy;
using Tracy.Messages;
using Tracy.Handlers;
using SharedLib.BasicServices.MessageFramework;
using System.IO;
using SharedLib.Exceptions;

namespace TracyGUI
{
    public partial class FormMain : Form
    {
        
        private delegate void UpdateUICallbackMessage(object sender, NotifyUIEventArgs e);//TODO: Move to General Lib
        private delegate void ExceptionCallbackMessage(object sender, ExceptionEventArgs e);//TODO: Move to General Lib

        private object _notificationLock = new object();
        private void MessageDispatcher_OnNotifyUI(object sender, NotifyUIEventArgs e)
        {
            if (this.IsDisposed) return;
            if (this.InvokeRequired)
            {
                UpdateUICallbackMessage d = new UpdateUICallbackMessage(MessageDispatcher_OnNotifyUI);
                this.Invoke(d, new object[] { sender, e });
            }
            else
            {
                lock (_notificationLock)
                {
                    switch (e.EventType)
                    {
                        case NotifyUIEventArgs.FOUND_RESOURCE:
                            OnFoundResource(e);
                            break;
                        case NotifyUIEventArgs.FILE_UPDATED :
                            OnFileUpdated(e);
                            break;
                    }
                }
            }
        }

        private void OnFileUpdated(NotifyUIEventArgs e)
        {
            List<MediaFile> fileList = e.Param as List<MediaFile>;
            foreach (MediaFile file in fileList)
            {
                ChangeMediaFileStatus(file, file.Status);
                if (file.Properties.ContainsKey(MediaFile.KEY_PREVIOUS_PATH))
                {
                    FileMoved(file);
                }
            }
        }

        private void OnFoundResource(NotifyUIEventArgs e)
        {
            List<Resource> resourceList = e.Param as List<Resource>;
            StringWriter sw = new StringWriter();
            foreach (Resource res in resourceList )
            {
                if (_mappingEntry.ContainsKey(res.Entry.ID))
                {
                    TracyBus.Instance.ResourceProvider.AddItem(res);
                    AddItemToResourceListView(res);
                    sw.WriteLine(res.Title);
                }
            }

            if (MessageBox.Show("The following new resources are detected, do you want to download them now?\n" + sw.ToString(), 
                "New Resource Found", 
                MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                foreach (Resource res in resourceList)
                {
                    if (_mappingEntry.ContainsKey(res.Entry.ID))
                    {
                        TracyBus.Instance.MessageDispatcher.EnqueueMessage(new TaskMessage(this, typeof(StartDownloadHandler), res, new MessageCallback(OnResourceSentToDownloader)));
                        TracyBus.Instance.MessageDispatcher.EnqueueMessage(new TaskMessage(this, typeof(AssignMediaFileHandler), res, new MessageCallback(OnResourceMediaFilesAssigned)));
                    }
                }
            }

        }

        FormConsole _formConsole;
        private void MessageDispatcher_OnException(object sender, SharedLib.Exceptions.ExceptionEventArgs e)
        {
            if (this.IsDisposed) return;
            if (this.InvokeRequired)
            {
                ExceptionCallbackMessage d = new ExceptionCallbackMessage(MessageDispatcher_OnException);
                this.Invoke(d, new object[] { sender, e });
            }
            else
            {
                if (_formConsole == null || _formConsole.IsDisposed) _formConsole = new FormConsole();
                _formConsole.AppendMessage(e.Message);
                _formConsole.AppendMessage(e.StackTrace);
                _formConsole.Show();
            }
            
        }
        
    }


}
